<template>
	<view class="shopping-details-address">
		<u-popup :show="show" mode="bottom" :overlay="true" round="30rpx" @open="open">
			<view class="content">
				<view class="" style="height: 32rpx;"></view>
				<view class="header">
					<view class="title">优惠卷</view>
					<view class="icon-close" @click="close">
						<u-icon name="close" size="12" color="#BDBDBD"></u-icon>
					</view>
				</view>
				<view class="" style="height: 40rpx;"></view>
				<scroll-view scroll-y="true" class="scroll-y">
					<view>
						<view class="list" v-for="(item,index) in listData1" :key="index" v-if="item.title">
							<view class="title">{{item.title}}</view>
							<view class="list-item" v-for="(i,o) in item.list" :key="o">
								<!-- 领取 -->
								<view style="display: flex;">
									<view class="box" :class="state == 1 ? 'receive' : ''">
										<view class="left">
											<view class="price"> {{i | couponFilter}}</view>
											<view class="time">使用时间：{{i | filTime}}</view>
										</view>
										<view class="right">
											<view class="price">
												<text>{{i.couponValue}}</text>
												<text style="font-size: 20rpx;margin-bottom:10rpx;">元</text>
											</view>
											<view class="btn" @click="handleReceive(i)" v-if="state == 1">立即领取</view>
											<view class="btn" @click="handleSubmit(index,o,'save')" v-if="state == 2">立即使用</view>
										</view>
									</view>
									<view v-if="state == 2" class="selector">
										<view class="selector-box" @click="handleSelector(index,o)">
											<selector v-model="i.show"></selector>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>

				<view class="submit" v-if="state == 2">
					<view class="box" @click="handleSubmit()">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		listDetailsReceive,
		listSaveReceive,
		listSureReceive
	} from "../http/home.js";
	import {
		dateFormat
	} from "@/utils/pay.js";
	import selector from "./selector.vue";
	export default {
		props: {
			shoppingPrice: {//商品总额
				type: Number, 
				default: () => 0
			},
			state: {//1领取 2使用
				type: Number,
				default: () => 2
			}
		},
		components: {
			selector
		},
		data() {
			return {
				show: false,
				openid: uni.getStorageSync('openid'),
				listData1: [],
			}
		},
		methods: {
			async init(id,listData) {
				try {
					uni.showLoading({
						title: "加载中...",
						mask: true
					});
					let res;
					if (this.state == 1) {
						res = await listDetailsReceive();
					} else {
						res = await listSureReceive({
							Ids: id,
							isUse: 0,
							phone: uni.getStorageSync('phone')
						});
						console.log('我的优惠卷：', res);
					}
					this.listData1 = res.data.rows.reduce((pev, item) => {
						item.show = false;
						if(Array.isArray(listData)){ //回显
							listData.forEach(o=>{
								if(o.id == item.id){
									item.show = true;
								}
							});							
						};
						if (Number(item.couponType) == 1 && item.minMonetary < this.shoppingPrice) { //满减
							if (!pev[0].list) pev[0].list = [];
							pev[0].list.push(item);
							pev[0].title = "满减"
						}
						if (Number(item.couponType) == 2) { //直减
							if (!pev[1].list) pev[1].list = [];
							pev[1].list.push(item);
							pev[1].title = "直减"
						}
						return pev
					}, [{}, {}]);
					
					
					console.log('查看listData1',this.listData1)
					if(!(this.listData1[0].list || this.listData1[1].list)){
						return uni.showToast({
							title:"没有可用的优惠卷",
							icon:"error"
						});
					};
					uni.hideLoading();
					this.show = true;
				} catch (e) {
					console.log('优惠卷查看内容', e)
				}
			},
			handleSelector(index,_index){
				if(Number(this.listData1[index].list[_index].issuperposition) == 1) return; //可以叠加
				this.listData1[index].list.forEach((i,x)=>{
					if(x != _index){
						if(Number(this.listData1[index].list[x].issuperposition) != 1){
							this.listData1[index].list[x].show = false;
						}
					}
				});
			},
			handleSubmit(index,_index,str) {
				if(str == 'save')  {
					this.listData1[index].list[_index].show = true;
					this.handleSelector(index,_index);
				};
				let list = []
				let couponTotal = this.listData1.reduce((pev,item)=>{
					 if(item.list){
						 item.list.forEach(i=>{
							 if(i.show){
								pev = pev + Number(i.couponValue);
								list.push(i);
							 }
						 })						 
					 }
					return pev;
				},0);
				this.$emit("total",couponTotal);
				this.$emit("getOptions",list);
				this.show = false;
			},
			async handleReceive(options) {
				console.log('查看当前优惠卷', options)
				let res = await listSaveReceive({
					userId: this.openid,
					couponId: options.couponId,
					couponNumber: options.couponNumber || 1,
					limitTime: options.limitTime
				})
				uni.showToast({
					title: "领取成功",
					icon: "success"
				});
			},
			open() {},
			close() {
				this.show = false
			}
		},
		filters: {
			filTime(options) {
				let str1 = dateFormat('YYYY年m.d', new Date(options.validiteStart.replaceAll('-', '/')));
				let str2 = dateFormat('m.d', new Date(options.validiteEnd.replaceAll('-', '/')));
				return `${str1}-${str2}`;
			},
			couponFilter(options) {
				if (Number(options.couponType) == 1) return '满' + options.minMonetary + '元使用';
				if (Number(options.couponType) == 2) return '直减' + options.couponValue + '元';
				// if (Number(options.useGoods) == 1) {
				// }
				// return '全品类商品';
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shopping-details-address {
		.selector {
			width: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		    .selector-box {
				width: 44rpx;
				height: 44rpx;
			}
		}

		.content {
			.scroll-y {
				max-height: 700rpx;
			}

			.submit {
				padding: 0 32rpx;
				padding-bottom: calc(env(safe-area-inset-bottom) + 38rpx);

				.box {
					width: 686rpx;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					background: #FD8F19;
					border-radius: 40rpx;
					font-size: 32rpx;
					font-weight: 400;
					color: #FFFFFF;
				}
			}

			.list {
				padding: 0 32rpx;
				padding-bottom: 100rpx;

				.title {
					padding-bottom: 24rpx;
					font-size: 20rpx;
					font-weight: 500;
					color: #1F1F1F;
				}

				.list-item {
					&+.list-item{
						margin-top: 14rpx;
					}
					.box {
						height: 162rpx;
						background: #FFFFFF;
						flex: 1;
						box-shadow: 0rpx 0rpx 6rpx 4rpx rgba(0, 0, 0, 0.05);
						border-radius: 16rpx;
						border-top: 12rpx solid #4D84C9;
						display: flex;

						.left {
							flex: 1;

							.price {
								font-size: 50rpx;
								font-weight: bold;
								color: #1F1F1F;
							}

							.time {
								width: 289rpx;
								height: 36rpx;
								line-height: 36rpx;
								border-radius: 18rpx;
								border: 2rpx solid #EEEEEE;
								text-align: center;
								font-size: 20rpx;
								font-weight: 500;
								color: #878787;
							}
						}

						.right {
							width: 188rpx;
							position: relative;

							&::after {
								position: absolute;
								top: 16rpx;
								bottom: 16rpx;
								left: 0;
								border-left: 2rpx dashed #EEEEEE;
							}

							.price {
								font-size: 62rpx;
								font-weight: 800;
								color: #4D84C9;
								display: flex;
								align-items: flex-end;
							}

							.btn {
								width: 132rpx;
								height: 30rpx;
								line-height: 30rpx;
								text-align: center;
								background: #4D84C9;
								border-radius: 16rpx;
								font-size: 22rpx;
								font-weight: bold;
								color: #FFFFFF;
								margin-top: 8rpx;
							}
						}

						.right,
						.left {
							display: flex;
							align-items: center;
							justify-content: center;
							flex-direction: column;
						}
					}

					.box.receive {
						border-color: #FD8F19;

						.right {
							.btn {
								background-color: #FD8F19;
								color: #FFFFFF;
							}

							.price {
								color: #FD8F19;
							}
						}
					}
				}
			}


			.header {
				position: relative;

				.title {
					width: 100%;
					text-align: center;
					font-size: 30rpx;
					font-weight: 500;
					color: #1F1F1F;
				}

				.icon-close {
					position: absolute;
					right: 32rpx;
					top: 50%;
					transform: translate(0, -50%);
					background-color: #F1F1F1;
					height: 38rpx;
					width: 38rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
</style>
